import router from '../router'

const mutations = {
  worktabRemove (state, p) {
    // 关闭标签
    const ind = state.worktab.list.findIndex(s => s.name === p)
    if (ind > -1) {
      // 清理 keep alive - start
      state.closingPage = state.worktab.list[ind].name
      // 清理 keep alive - end
      state.worktab.list.splice(ind, 1)
    }
    if (p === state.worktab.current.name) {
      // 是当前页，返回上一页
      const tabsLength = state.worktab.list.length
      router.push(state.worktab.list[tabsLength - 1].path)
    }
  },
  worktabRoute (state, p) {
    const ind = state.worktab.list.findIndex(s => s.name === p.to.name)
    if (ind > -1) {
      // 标签已存在
      state.worktab.current = state.worktab.list[ind]
    } else {
      // 标签不存在，现在新建
      state.worktab.list.push(p.to)
      state.worktab.current = p.to
    }
    state.closingPage = ''
  },
  ClearTabs(state) {
    state.worktab.list = []
  }
}
export default mutations
